from Tkinter import Tk
import numpy as np
def p():
    t=Tk().clipboard_get()
    return pst(t)
def pst(string):
    if string.find('[') == -1:
        return float(string)
    if string.find("*") == -1:
        string = string[1:-1]
        matrix = []
        for st in string.split(';'):
            fila = []
            for elem in st.split():
                fila.append(float(elem))
            matrix.append(fila)
        dimX = len(matrix)
        dimY = len(matrix[0])
        out = np.zeros((dimX, dimY))
        out = np.array(matrix)
        return out
    else:
        mStr = string.split('*')
        listaMatrices = []
        for m in mStr:
            m = m[1:-1]
            matrix = []
            for st in m.split(';'):
                fila = []
                for elem in st.split():
                    fila.append(float(elem))
                matrix.append(fila)
            listaMatrices.append(matrix)
        dim3 = len(listaMatrices)
        dimX = len(listaMatrices[0])
        dimY = len(listaMatrices[0][0])
        out = np.zeros((dimX, dimY, dim3))
        for i in range(dim3):
            out[:, :, i] = np.array(listaMatrices[i])
        return out

def cmp(toComp):
    return np.array_equal(toComp, p())

def cmpT(toComp):
    return np.array_equal(np.transpose(toComp), p())
def cmpC(toComp):
    return np.allclose(toComp, p(),rtol=1e-02)
def cmpCT(toComp):
    return np.allclose(np.transpose(toComp), p())

def parseStruct(stringStr):
    newObj = {}
    tupes = stringStr.split("///")

    for stringT in tupes:
        if stringT == "":
            continue
        alm = stringT.split(":")
        newObj[alm[0]] = pst(alm[1])
    return newObj

def fullTest(ma1,ma2,tol=1e-02,debug=False):
    if ma1.shape != ma2.shape:
        return False
    t1 = np.array_equal(ma1, ma2)
    t2 = np.allclose(ma1, ma2,atol=tol)
    assert((not(t1) and not(t2)) or t1 or t2)
    if debug:
        print "Equals ",t1
        print "Close 1e-02 ",t2
        print "Max abs(a-n) ",np.max(np.abs(ma1-ma2))
        print "Result ",(t1 or t2)
    return (t1 or t2)
def getIndexOfRow(matrix, row):
    return np.where(np.all(matrix == row, axis=1))
def checkIfPerm(matrix1,matrix2):
    correspondencia = [getIndexOfRow(row, matrix1) for row in matrix2]
    sonEmpty = map(lambda x: len(x[0]), correspondencia)
    return reduce(lambda r1,r2 : r1 and r2, sonEmpty)

#from parser import p,cmp,cmpT,cmpC,cmpCT
"""
Uso
from utils.parser import *
fullTest(p(),X)
Donde se quiere comprobar que el valor copiado desde matlab(conseguido por p())
es igual a X
"""

if __name__ == "__main__":

    testStruct = "frames:[1 2]///matches:[104.55908203125 92.0968780517578 38.6970825195313 -24.7012939453125 -33.0030822753906 -35.4376831054688 -40.0213623046875 -112.054016113281 190.609760284424 76.6166076660156 72.4523162841797 23.0695953369141 -4.47067260742188 -13.7249603271484 -19.1539611816406 -34.3994750976563 -34.3994750976563 -40.783203125 -57.9983520507813 -78.2849426269531 -93.5857543945313 -136.96533203125 182.81184387207 116.188339233398 85.9875640869141 85.9875640869141 70.9907989501953 71.5550079345703 12.0646514892578 -5.68087768554688 -33.1934509277344 -81.1534118652344 -81.1534118652344 -6.27102661132813 -60.4061584472656 109.557891845703 12.2635498046875 -4.41680908203125 -4.41680908203125 -46.3473205566406 48.0803833007813 29.5469055175781 24.6024475097656 -21.559326171875;-169.703826904297 -210.972595214844 -214.204833984375 86.4859466552734 174.037139892578 -176.931030273438 -201.952514648438 -113.366912841797 296.570676803589 212.845184326172 134.630432128906 -193.782165527344 -216.070617675781 172.782928466797 149.472457885742 180.580520629883 180.580520629883 204.784965515137 -178.880767822266 -126.283264160156 -158.542205810547 -187.055725097656 301.770374298096 300.114555358887 -164.170379638672 -164.170379638672 168.964645385742 122.728820800781 159.390045166016 244.405693054199 153.297943115234 80.5236663818359 80.5236663818359 137.858520507813 -272.312622070313 -153.662170410156 171.588638305664 216.551078796387 216.551078796387 116.824081420898 147.335647583008 138.101333618164 107.753677368164 166.391799926758;122.296630859375 122.853096008301 77.1413726806641 13.1478881835938 8.95669555664063 -5.11518859863281 0.473953247070313 -108.58642578125 41.9714813232422 105.570739746094 115.571823120117 57.2670745849609 39.31201171875 28.0209655761719 19.4946594238281 6.22746276855469 6.22746276855469 -1.57060241699219 -27.008544921875 -68.9511413574219 -69.8277282714844 -124.752655029297 34.7072143554688 -28.4292602539063 103.559158325195 103.559158325195 110.076217651367 113.026176452637 56.6191864013672 28.5378265380859 4.1578369140625 -51.7969665527344 -51.7969665527344 29.6095123291016 2.35060119628906 120.404357910156 56.6713714599609 32.8003845214844 32.8003845214844 -14.0986633300781 88.6640625 68.3035888671875 63.8470458984375 20.8484649658203;-154.039306640625 -195.681457519531 -206.924194335938 84.9102630615234 174.001586914063 -181.541473388672 -207.793701171875 -128.435668945313 307.746163368225 217.97972869873 140.422317504883 -189.694274902344 -216.906921386719 173.055801391602 150.450836181641 180.296676635742 180.296676635742 205.346969604492 -186.964080810547 -137.448791503906 -171.301025390625 -196.00732421875 312.666220188141 308.584372520447 -150.715698242188 -150.715698242188 174.385787963867 127.80012512207 160.913101196289 245.769630432129 151.675430297852 75.2618408203125 75.2618408203125 136.95751953125 -282.993469238281 -137.334350585938 173.808364868164 217.878456115723 217.878456115723 114.759887695313 150.981353759766 140.516159057617 110.540878295898 166.806503295898]///F:[-1.05410048833945e-08 1.56356056795821e-07 -0.000104727065457066;-1.49334752816059e-06 2.90199408818357e-07 -0.00559477766743047;0.00064672506157313 0.00561546246388563 -0.00420600192824178]///E:[-0.00545756629992145 0.0809527702336744 -0.0753559305686634;-0.773175160614827 0.150249670818307 -4.02569933201115;0.465348366482193 4.0405829924928 -0.00420600192824178]///Rt:[0.980507892602122 -0.0944431127590168 -0.172292689912637 0.99963836870063;0.0976425288006969 0.995169579044828 0.0101708167393987 -0.0186917502406422;0.17049988010069 -0.0267956600241344 0.984993291088585 -0.0193326225454708]///f:719.5459///Mot:[0.999879130339609 0.00880498301125505 -0.0128139371575655 0.0966678885506464;-0.00888685806898615 0.999940369930756 -0.00634667916355482 -0.069978450682811;0.0127572906593925 0.0064597876834239 0.999897756112152 -3.51577617509314]*[0.980636494759034 -0.0848521010283081 -0.176499818973837 0.916986481160674;0.0852828285792061 0.996343432040814 -0.00515796264643823 -0.0706249414988878;0.176292099358551 -0.00999431739610548 0.984287157959272 -3.44237398527039]///Str:[0.166697945999278 0.130788190103594 0.033005790385496 -0.0760660105597759 -0.0866487129237495 -0.0969424742146559 -0.101955131658391 -0.266975060520048 29.679212861703 0.0652305349526345 0.0558478543963418 0.00710883712614587 -0.0415269750760221 -0.0606512159394499 -0.068123540220618 -0.0887717021824449 -0.0887717021824241 -0.0974691534701273 -0.137253997194628 -0.19139502440855 -0.206879391091686 -0.311397147720851 20.8457202791373 8.4806488350039 0.132580558777547 0.132580558941708 0.0541129197148793 0.0564703870321124 -0.0259530906380897 -0.0506403496469039 -0.0880213860654382 -0.163561911443076 -0.163561911443204 -0.0501057704796098 -0.127475006868633 0.186120772473202 -0.0259514343028657 -0.0485894100586125 -0.0485894100662701 -0.108742411428512 0.0239717694729195 -0.000230130418343862 -0.00604526988793999 -0.0711337828450526;-0.219043607438998 -0.271889416021196 -0.266597199966755 0.223256073932415 0.332996741988701 -0.219532526992627 -0.245361461838684 -0.141086502522958 45.7626592115044 0.397720737941238 0.280367625480153 -0.239640075909824 -0.262488285389485 0.331278969805976 0.306992716912077 0.342629366039846 0.342629366044183 0.374478215086936 -0.222618124712463 -0.159960652146311 -0.196979420516426 -0.277472375091828 34.084348228294 21.3228096152714 -0.210584776591545 -0.21058477675396 0.327977547933647 0.26656022641832 0.310906689164935 0.429472074090974 0.3121936540728 0.22112251438638 0.221122514384929 0.293496587131343 -0.321094469117112 -0.20043875278997 0.326335812496437 0.391142066415378 0.391142066397444 0.270388685317677 0.299769438930537 0.290652589824944 0.251080462821295 0.323014996190975;4.88303988000562 4.79358755951129 4.75178312108423 4.55886616603378 4.48655851162813 4.81392506359306 4.74239322569142 5.03455993593737 111.392497007316 4.52197690320794 4.48227407262891 4.77941771448688 4.71985998669983 4.4872479749785 4.51754089931871 4.49047408147902 4.49047408148802 4.48565757088674 4.80993375343906 4.98810763745147 4.85818058083656 5.00672821615129 82.4769476861183 53.3268324286072 4.89260620377052 4.89260620441218 4.48788973639764 4.4979488494814 4.47577948706275 4.49077589436965 4.52374813884181 4.61728275995637 4.61728275995084 4.53942020045568 4.62147342739383 4.94340644887821 4.47043239837595 4.48860400433432 4.48860400429316 4.57675689203552 4.49678835103818 4.51644739823863 4.53127591556444 4.48750468246158]///ObsVal:[104.55908203125 92.0968780517578 38.6970825195313 -24.7012939453125 -33.0030822753906 -35.4376831054688 -40.0213623046875 -112.054016113281 190.609760284424 76.6166076660156 72.4523162841797 23.0695953369141 -4.47067260742188 -13.7249603271484 -19.1539611816406 -34.3994750976563 -34.3994750976563 -40.783203125 -57.9983520507813 -78.2849426269531 -93.5857543945313 -136.96533203125 182.81184387207 116.188339233398 85.9875640869141 85.9875640869141 70.9907989501953 71.5550079345703 12.0646514892578 -5.68087768554688 -33.1934509277344 -81.1534118652344 -81.1534118652344 -6.27102661132813 -60.4061584472656 109.557891845703 12.2635498046875 -4.41680908203125 -4.41680908203125 -46.3473205566406 48.0803833007813 29.5469055175781 24.6024475097656 -21.559326171875 122.296630859375 122.853096008301 77.1413726806641 13.1478881835938 8.95669555664063 -5.11518859863281 0.473953247070313 -108.58642578125 41.9714813232422 105.570739746094 115.571823120117 57.2670745849609 39.31201171875 28.0209655761719 19.4946594238281 6.22746276855469 6.22746276855469 -1.57060241699219 -27.008544921875 -68.9511413574219 -69.8277282714844 -124.752655029297 34.7072143554688 -28.4292602539063 103.559158325195 103.559158325195 110.076217651367 113.026176452637 56.6191864013672 28.5378265380859 4.1578369140625 -51.7969665527344 -51.7969665527344 29.6095123291016 2.35060119628906 120.404357910156 56.6713714599609 32.8003845214844 32.8003845214844 -14.0986633300781 88.6640625 68.3035888671875 63.8470458984375 20.8484649658203;-169.703826904297 -210.972595214844 -214.204833984375 86.4859466552734 174.037139892578 -176.931030273438 -201.952514648438 -113.366912841797 296.570676803589 212.845184326172 134.630432128906 -193.782165527344 -216.070617675781 172.782928466797 149.472457885742 180.580520629883 180.580520629883 204.784965515137 -178.880767822266 -126.283264160156 -158.542205810547 -187.055725097656 301.770374298096 300.114555358887 -164.170379638672 -164.170379638672 168.964645385742 122.728820800781 159.390045166016 244.405693054199 153.297943115234 80.5236663818359 80.5236663818359 137.858520507813 -272.312622070313 -153.662170410156 171.588638305664 216.551078796387 216.551078796387 116.824081420898 147.335647583008 138.101333618164 107.753677368164 166.391799926758 -154.039306640625 -195.681457519531 -206.924194335938 84.9102630615234 174.001586914063 -181.541473388672 -207.793701171875 -128.435668945313 307.746163368225 217.97972869873 140.422317504883 -189.694274902344 -216.906921386719 173.055801391602 150.450836181641 180.296676635742 180.296676635742 205.346969604492 -186.964080810547 -137.448791503906 -171.301025390625 -196.00732421875 312.666220188141 308.584372520447 -150.715698242188 -150.715698242188 174.385787963867 127.80012512207 160.913101196289 245.769630432129 151.675430297852 75.2618408203125 75.2618408203125 136.95751953125 -282.993469238281 -137.334350585938 173.808364868164 217.878456115723 217.878456115723 114.759887695313 150.981353759766 140.516159057617 110.540878295898 166.806503295898]///ObsIdx:[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44;45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88]///"
    t=parseStruct(testStruct)
    print "hi"

    pass